
#include "sshtml.h"

SSHTML::SSHTML():
    SSDOM(),
    table_open(false),
    table_row_open(false)
{
    background_color[0] = 0;
    width[0] = 0;
    strcpy(align,"center");
    makeHtml();
}

SSHTML::SSHTML(const char *file):
    SSDOM(file),
    table_open(false),
    table_row_open(false)
{
    background_color[0] = 0;
    width[0] = 0;
    strcpy(align,"center");
}

SSHTML::~SSHTML()
{
}

void SSHTML::makeHtml()
{
    //if(!started)
    {
        //started = true;
        startTagCB(NULL,GC_HTML,NULL);
        startTagCB(NULL,GC_HEAD,NULL);
        endTagCB(NULL,GC_HEAD);
        SSAttribute attr;
        if(0 != background_color[0])
        {
            attr.add(GC_BGCOLOR,background_color); // set the background color
        }
        startTagCB(NULL,GC_BODY,&attr);
    }
}

void 
SSHTML::makeParagraph(const char *data,SSLeaf *n)
{
    startTagCB(NULL,GC_PARAGRAPH,NULL);
    charDataCB(NULL,data);
    endTagCB(NULL,GC_PARAGRAPH);
}

void 
SSHTML::openParagraph()
{
    startTagCB(NULL,GC_PARAGRAPH,NULL);
}

void 
SSHTML::closeParagraph()
{
    endTagCB(NULL,GC_PARAGRAPH);
}

void
SSHTML::makeData(const char* data,SSLeaf *n)
{
    charDataCB(NULL,data);
}

void
SSHTML::makeLink(const char* file, const char* data,SSLeaf *n)
{
    SSAttribute attr;
    attr.set(GC_REFERENCE,file);
    startTagCB(NULL,"A",&attr);
    charDataCB(NULL,data);
    endTagCB(NULL,"A");
}

void 
SSHTML::makeHeading(const char *heading,SSLeaf *n,unsigned level)
{
    if( (0 < level) && (7 > level) )
    {
        char tag[3];
        tag[0] = 'H';
        tag[2] = '\0';
        switch(level)
        {
            case 1:
                tag[1] = '1';
            break;
            case 2:
                tag[1] = '2';
            break;
            case 3:
                tag[1] = '3';
            break;
            case 4:
                tag[1] = '4';
            break;
            case 5:
                tag[1] = '5';
            break;
            case 6:
                tag[1] = '6';
            break;
        }
        SSAttribute att;
        att.add("ALIGN", align);
        startTagCB(NULL,tag,&att);
        charDataCB(NULL,heading);
        endTagCB(NULL,tag);
    }
}

void
SSHTML::makeImage(const char* imge)
{
    SSAttribute att;
    att.add("ALIGN", align);
    startTagCB(NULL,"DIV",&att);
    SSAttribute att_img;
    att_img.add("SRC", imge);
    startTagCB(NULL,"IMG",&att_img);
    endTagCB(NULL,"IMG");
    endTagCB(NULL,"DIV");
}

void
SSHTML::gotoBody()
{
    SSLeaf *top_leaf = searchNext(getHead(),GC_BODY);
    if(top_leaf)
    {
        setCurrent(top_leaf);
    }
    else
    {
        throw "SSHTML::gotoBody No body in file";
    }
}

void 
SSHTML::setTable(SSLeaf *leaf)
{
    table = leaf;
}

void 
SSHTML::setTableRow(SSLeaf *leaf)
{
    if(table_row_open)
    {
        row = leaf;
    }
    else 
    {
        throw "SSHTML::setTableRow Table is not open";
    }
}

void SSHTML::openTable(const char *heading)
{
    if(!table_open)
    {
        SSAttribute att;
        att.add("BORDER", "1");
        if(0 != width[0])
        {
            att.add("WIDTH",width);
        }
        startTagCB(NULL,GC_TABLE,&att);
        if(heading)
        {
            //att.add("title",heading);
            startTagCB(NULL,GC_CAPTION,NULL);
            charDataCB(NULL,heading);
            endTagCB(NULL,GC_CAPTION);
        }
        table = getCurrent();
    }
    else 
    {
        throw "SSHTML::openTable Table is allready opened";
    }
}

void SSHTML::openTableRow()
{
    if(!table_row_open)
    {
        SSAttribute att;
        att.add("ALIGN", align);
        startTagCB(NULL,GC_TABLE_ROW,&att);
        row = getCurrent();
        table_row_open = true;
    }
    else 
    {
        throw "SSHTML::openTableRow Row is allready opened";
    }
}

void SSHTML::closeTable()
{
    if(table_open)
    {
        endTagCB(NULL,GC_TABLE);
        table = 0;
        table_open = false;
    }
}

void SSHTML::getNextTable(SSLeaf *leaf)
{
    table = searchNext(leaf,GC_TABLE);
}

void 
SSHTML::getNextRow(SSLeaf *leaf)
{
    row = searchNext(leaf,GC_TABLE_ROW);
}

void 
SSHTML::closeTableRow()
{
    if(table_row_open)
    {
        endTagCB(NULL,GC_TABLE_ROW);
        row = 0;
        table_row_open = false;
    }
}

void 
SSHTML::setTableData(const char *data)
{
    setCurrent(row);
    startTagCB(NULL,GC_TABLE_DATA,NULL);
    charDataCB(NULL,data);
    endTagCB(NULL,GC_TABLE_DATA);
}

void 
SSHTML::setTableHeader(const char *heading)
{
    setCurrent(row);
    startTagCB(NULL,GC_TABLE_HEADER,NULL);
    charDataCB(NULL,heading);
    endTagCB(NULL,GC_TABLE_HEADER);
}

void
SSHTML::endHtml()
{
    endTagCB(NULL,GC_BODY);
    endTagCB(NULL,GC_HTML);
}

void
SSHTML::writeHtml()
{
    readyWrite();
    write("bla");
    closeOutFile();
}

void
SSHTML::setBackgroundColor(const char *color)
{
    memcpy(background_color,color,6);
    background_color[6]=0;
}

void
SSHTML::setWidth(const char *w)
{
    strcpy(width,w);
}
